let app = getApp();
Page({
  data: {
    goodsData: {},
    videoUrl: null,
    listmiaoshuurl: [],
    shopId: 0,
    openid: '',
    delog:false,
    delogFlag:'',
    count: 1,
    indicatorDots: true,
    vertical: true,
    autoplay: true,
    interval: 3000,
    duration: 500,
    swiperImgs: [],
    detailImgs: [],
    totalPrice: 0,
    collect: false
  },
  onLoad(option) {
    let that = this;
    wx.showLoading({
      title: '请稍等...',
    });
    //判断将业务员id存储起来
    let salOpenid = option.salesmanOpenid;
    if (salOpenid) {
      wx.setStorage({
        key: 'salesmanOpenid',
        data: salOpenid,
      });
    }
    wx.getStorage({
      key: 'openid',
      success(res) {
        that.setData({
          openid: res.data,
          shopId: option.shopId
        });
        that.ajaxShop();
      },
      fail(res) {
        wx.reLaunch({
          url: '/pages/logs/logs?shopFlag=cart&shopId=' + option.shopId,
        });
      }
    });

  },
  ajaxShop() {
    let that = this,
      collectType = false;
    app.get('GetShangPinNew', {
      action: 'getbyand',
      openid: that.data.openid,
      id: that.data.shopId
    }).then((res) => {
      let videoList = res.listurl,
        videoUrl = '';
      for (let i in videoList) {
        if (videoList[i].LeiBie === '视频') {
          videoUrl = videoList[i].Url;
        }
      }
      res.ShouCang === '收藏' ? collectType = true : collectType = false;
      wx.hideLoading();
      that.setData({
        swiperImgs: res.listurl,
        goodsData: [res],
        videoUrl: videoUrl,
        listmiaoshuurl: res.listmiaoshuurl,
        totalPrice: res.DanJia,
        collect: collectType
      });
    })
  },
  jumpCart(e) {
    let pageFlag = e.currentTarget.id,
      url = '/pages/detail/detail';
    if (pageFlag == 'home') {
      url = '/pages/home/home'
    } else if (pageFlag == 'cart') {
      url = '/pages/cart/cart'
    }
    wx.reLaunch({
      url: url,
    })
  },
  //添加购物车
  addCat() {
    let that = this;
    app.get('ShouChang', {
      action: 'insert',
      shuliang:that.data.count,
      openid: that.data.openid,
      shangpinid: that.data.shopId,
      leibie: '购物车'
    }).then((res) => {
      if (res.msg === 'ok！') {
        app.toast('加入购物车成功');
      } else {
        app.toast('加入购物车失败');
      }
    });
  },
  //加入收藏
  addCollect(e) {
    let that = this,
      actionType = '',
      collectFlag = null,
      collectType = e.currentTarget.id;
    if (collectType === 'addCollect') {
      actionType = 'insert';
    } else if (collectType === 'deleteColl') {
      actionType = 'del';
    }
    collectFlag = !this.data.collect;
    that.setData({
      collect: collectFlag
    });
    app.get('ShouChang', {
      action: actionType,
      openid: that.data.openid,
      shangpinid: that.data.shopId,
      leibie: '收藏'
    }).then((res) => {
      res.msg === 'ok！' ? app.toast('操作成功') : app.toast('操作失败');
    });
  },
  //立即购买
  offBuy() {
    let priceAll = this.data.totalPrice,
      info = this.data.shopId + "|" + this.data.count;
    wx.navigateTo({
      url: '/pages/submitOrder/submitOrder?sumAll=1' + '&priceAll=' + priceAll + '&info=' + info + '&typeInt=detail' + '&shopId=' + this.data.shopId
    });
   
  },
  changeIndicatorDots(e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay(e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange(e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange(e) {
    this.setData({
      duration: e.detail.value
    })
  },
  //分享
  onShareAppMessage(options) {
    let data = this.data.goodsData,
      img = this.data.swiperImgs;
    return {
      title: data.MingCheng,
      path: '/pages/detail/detail?shopId=' + this.data.shopId + '&salesmanOpenid=' + this.data.openid,
      imgUrl: img[0].Url,
      success(res) {
        if (res.errMsg == 'shareAppMessage:ok') {
          app.toast('分享成功');
        }
      },
      fail(res) {
        console.log(res)
        if (res.errMsg == 'shareAppMessage:fail cancel') {
          app.toast('取消分享');
        } else if (res.errMsg == 'shareAppMessage:fail') {
          app.toast('分享失败');
        }
      }
    }
  },
  /**
   * 用户点击商品减1
   */
  subtracttap(e) {
    var count = this.data.count;
    if (count <= 1) {
      return;
    } else {
      count--;
      this.setData({
        'count': count
      });
    }
  },

  /**
   * 用户点击商品加1
   */
  addtap(e) {
    let count = this.data.count,
      kucun = e.currentTarget.dataset.kucun;
    if (count > 98) {
      this.setData({
        'count': 99
      });
      return false;
    }
    //大于库存
    if (count >= kucun) {
      this.setData({
        'count': kucun
      });
      app.toast('存库不足');
      return false;
    }
    count++;
    this.setData({
      'count': count
    });
  },
  countInput(e) {
    let count = e.detail.value,
      kucun = e.currentTarget.dataset.kucun;
      //大于库存
    if (count >= kucun) {
      this.setData({
        'count': kucun
      });
      app.toast('存库不足');
      return false;
    }
    if (count > 98) {
      this.setData({
        'count': 99
      });
      return false;
    }
   
    this.setData({
      'count': count
    });
  },
  addCatBuy(e){
    let flag = e.currentTarget.id;
    this.setData({
      delog: true,
      delogFlag: flag
      });    
  },
  close(){
    this.setData({
      delog: false
    })
  },
  confriom(){
    let delogFlag = this.data.delogFlag;
    if (delogFlag =='addCat'){
      this.addCat();
    } else if (delogFlag == 'offBuy'){
      this.offBuy();
    }
    this.setData({
      delog: false
    })
  }
})